//引入WxValidate验证类
import WxValidate from '../../utils/WxValidate';
//获取应用实例
const app = getApp()
let ServiceUrl = app.globalData.ServiceUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type_id:'',
    order_id:'',
    array: ['无', '有'],
    index:0,
    reg_time: '',
    form:{
      patient_name:'',
      IDcard:'',
      phone:'',
      disease:'',
      symptom:'',
      hospital:'',
      offices:'',
      expert:'',
      others:''
    }
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      reg_time: e.detail.value
    })
  },
  bindPickerChange: function(e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    console.log(wx.getStorageSync('roleState'))
    this.setData({
      user_id: wx.getStorageSync('uid'),
      type_id: options.typeid,
      role_state: wx.getStorageSync('roleState')
    });
    this.initValidate()
  },
  //报错
  showModal(error){
    wx.showModal({
      content:error.msg,
      showCancel: false,
      confirmColor: 'skyblue',
    })
  },
//验证函数
initValidate(){
  const rules = {
    patient_name:{
      required: true,
      rangelength: [2,4]
    },
    IDcard:{
      required: true,
      idcard: true
    },
    phone:{
      required: true,
      tel:true
    },
    disease:{
      required: true
    },
    symptom:{
      required:true
    },
    hospital:{
      required:true
    },
    offices:{
      required:true
    },
    expert:{
      required:true
    },
  }
  const messages={
    patient_name:{
      required: '请输入姓名',
      rangelength:'请输入2~4个文字的姓名'
    },
    IDcard:{
      required: '请输入身份证号码',
      idcard: '请输入正确的身份证号码'
    },
    phone:{
      required: '请输入手机号',
      tel: '请输入正确的手机号'
    },
    disease:{
      required: "请输入所患疾病"
    },
    symptom:{
      required:"请输入目前症状"
    },
    hospital:{
      required:"请输入想要预约的医院"
    },
    offices:{
      required:"请输入所属科室"
    },
    expert:{
      required:"请输入就诊专家"
    }
  }
  this.WxValidate = new WxValidate(rules,messages)
},
//调用验证函数
formSubmit: function(e){
  let that = this;
  console.log('form发生了submit事件，携带的数据为：',e.detail.value)
  const params = e.detail.value;
  //校验表单
  if(!this.WxValidate.checkForm(params)){
    const error = this.WxValidate.errorList[0]
    this.showModal(error)
    return false
  }
  wx.showLoading({
    title: '提交中',
  })
  setTimeout(res=>{
    wx.hideLoading();
  },1000)
  //console.log(that.options.uid)//用户user_id
  //提交数据
  wx.request({
    url: ServiceUrl + 'putOrder',
    data:{
      user_id:that.data.user_id,
      type_id:e.detail.value.type_id,
      patient_name:e.detail.value.patient_name,
      IDcard:e.detail.value.IDcard,
      phone:e.detail.value.phone,
      disease:e.detail.value.disease,
      symptom:e.detail.value.symptom,
      hospital:e.detail.value.hospital,
      reg_time:that.data.reg_time,
      offices:e.detail.value.offices,
      expert:e.detail.value.expert,
      others:e.detail.value.others,
    },
    method: "post",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    success:function(res){
      console.log(res)
      wx.hideLoading();
      if(res.data.state == 200){
        wx.showModal({
          title: "提示",
          content: res.data.msg,
          showCancel: false,
          confirmColor: 'skyblue',
          success:function(re){
            if(res.data.role_state == 2){
              //如果是管理员提交的，则直接发给老汤
              that.sendMsgToBoss(res.data.order_id)
            }else{
              //发送给管理员请求审核
              that.subscribeMessage(res.data.order_id);
            }
            if(re.confirm){
              wx.redirectTo({
                url: '../details/assistance?order_id='+res.data.order_id+'&role_state='+res.data.role_state,
              })
            }
          }
        })
      }else if(res.data.state == 400){
        wx.showModal({
          cancelColor: 'cancelColor',
          content: res.data.msg,
          showCancel: false,
          confirmColor: 'skyblue',
          success (res){
            if(res.confirm){
              wx.navigateBack({
                delta: 0,
              })
            }
          }
        });
      }
    },
    fail:function(res){
      wx.showModal({
        title:"提示",
        content:"发送请求失败",
        showCancel: false,
        confirmColor: 'skyblue',
      })
    }
  })
},
/**
 * 发送助医服务通过审核消息给老汤，让他设置金额
 * @param {*} order_id 
 */
sendMsgToBoss:function(order_id){
  wx.request({
    url: ServiceUrl + 'sendMsgToBoss',
    data: {order_id: order_id},
    method: "POST",
    success: function(res){
      console.log(res)
    },
    fail: function(){
      console.log('发送请求失败')
    }
  })
},
//发给管理员的订单提醒消息
subscribeMessage:function(order_id){
  let that = this
  wx.request({
    url: ServiceUrl + 'subscribeMessage',
    data: {order_id: order_id},
    method: "get",
    success: function(res){
      console.log(res)
    },
    fail:function(){
      console.log('发送请求失败')
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})